package com.dys.testtreemap03;

import java.util.Comparator;
import java.util.TreeMap;

/**
 * @className: com.dys.testtreemap03.TreeMapTest
 * @description: com.dys.testtreemap03
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/08/08 下午10:40
 */
public class TreeMapTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        new TreeMapTest().testStudent3();
    }

    /**
     * 唯一，无序,key不能为null
     */
    public void testTreeMap() {
        TreeMap<String, Integer> tm = new TreeMap<>();
        // tm.put(null,null);
        tm.put("one", 1);
        tm.put("two", 2);
        tm.put("three", 3);
        tm.put("four", 4);
        tm.put("five", 5);
        tm.put("six", 6);
        tm.put("seven", 7);
        tm.put("two", 8);
        System.out.println(tm.size());
        System.out.println(tm);
    }

    /**
     * key的类型是一个自定义的引用数据类型: 内部比较器
     */
    public void testStudent() {
        TreeMap<Student, Integer> tm = new TreeMap<>();
        tm.put(new Student("alili", 22, 186.5), 1000);
        tm.put(new Student("blili", 24, 186.5), 1000);
        tm.put(new Student("clili", 23, 186.5), 1000);
        tm.put(new Student("dlili", 25, 186.5), 1000);
        tm.put(new Student("elili", 27, 186.5), 1000);
        tm.put(new Student("blili", 28, 186.5), 1000);
        System.out.println(tm.size());
        System.out.println(tm);
    }

    /**
     * 外部比较器
     */
    public void testStudent2() {
        TreeMap<Student, Integer> tm = new TreeMap<>(new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getHeight().compareTo(o2.getHeight());
            }
        });
        tm.put(new Student("alili", 22, 186.5), 1000);
        tm.put(new Student("blili", 24, 176.5), 1000);
        tm.put(new Student("clili", 23, 166.5), 1000);
        tm.put(new Student("dlili", 25, 156.5), 1000);
        tm.put(new Student("elili", 27, 146.5), 1000);
        tm.put(new Student("blili", 28, 186.5), 1000);
        System.out.println(tm.size());
        System.out.println(tm);
    }

    public void testStudent3() {
        TreeMap<Student, Integer> tm = new TreeMap<>(new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        tm.put(new Student("alili", 22, 186.5), 1000);
        tm.put(new Student("blili", 24, 176.5), 1000);
        tm.put(new Student("clili", 23, 166.5), 1000);
        tm.put(new Student("dlili", 25, 156.5), 1000);
        tm.put(new Student("elili", 27, 146.5), 1000);
        tm.put(new Student("blili", 28, 186.5), 1000);
        System.out.println(tm.size());
        System.out.println(tm);
    }
}
